import os

IMAGESUFFIXES = ['png','jpeg','jpg','bmp']


def get_images_by_listdir(base_dir):
    data = []
    for image_path in os.listdir(base_dir):
        if image_path.endswith(suffix for suffix in IMAGESUFFIXES):
            data.append(os.path.join(base_dir,image_path))
    return data

def get_images_by_walkdir(base_dir):
    data = []
    for root,_,files in os.walk(base_dir):
        for file in files:
            if file.endswith(suffix for suffix in IMAGESUFFIXES):
                data.append(os.path.join(root,file))
    return data

def collect_data(dirs,method='walk'):
    assert method in ['walk','list']
    if method == 'walk':
        func = get_images_by_walkdir
    else:
        func = get_images_by_listdir
    if isinstance(dirs,list):
        data = []
        for dir in dirs:
            data.extend(func(dir))
    else:
        data = func(dirs)
    return data


def eval_pipeline(pipeline):
    module_type = pipeline.pop('type')
    module = eval(module_type)(**pipeline)
    return module